package com.qf.util;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: AliyunOSSUtil
 * @Author: WangHao
 * @Date: 2025/4/29 19:07
 * @Description: 阿里云OSS文件上传工具类
 * @Version: 1.0
 */

public class AliyunOSSUtil {
    /**
     * 上传文件
     */
    public static String upLoad(MultipartFile file) {
        System.out.println("------OSS文件上传开始--------" );

        String endPoint = AliyunOSSConstants.END_POINT;
        String accessKeyId = AliyunOSSConstants.ASSCESS_KEY_ID;
        String accessKeySecret = AliyunOSSConstants.ASSCESS_KEY_SECRET;
        String bucketName = AliyunOSSConstants.BUCKET_NAME;
        String fileDir = AliyunOSSConstants.FILE_DIR;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(new Date( ));
        String fileUrl = null; // 存不带文件名的后半截路径
        String fileUrl1 = null; // 存带文件名的后半截路径
        OSS client = new OSSClientBuilder( ).build(endPoint, accessKeyId, accessKeySecret);
        try {
            // 判断容器是否存在,不存在就创建
            if (!client.doesBucketExist(bucketName)) {
                client.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                client.createBucket(createBucketRequest);
            }
            String originalFilename = file.getOriginalFilename( );
            String suffix = originalFilename.substring(originalFilename.indexOf("."));
            // 设置文件路径和名称
            // fileUrl = fileDir + "/" + (dateStr + "/" + UUID.randomUUID( ).toString( ).replace("-", "") + suffix);
            fileUrl = (dateStr + "/" + UUID.randomUUID( ).toString( ).replace("-", "") + suffix);
            fileUrl1 = fileDir + "/" + fileUrl;
            System.out.println("fileUrl：" + fileUrl);
            // 上传文件
            PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl1, file.getInputStream( )));
            // 设置权限(公开读)
            client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            if (result != null) {
                System.out.println("resp：" + result);
                System.out.println("------OSS文件上传成功------" + AliyunOSSConstants.COMMON_URL + fileUrl);
            }
        } catch (OSSException oe) {
            oe.printStackTrace();
        } catch (ClientException ce) {
            ce.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace( );
        } finally {
            if (client != null) {
                client.shutdown( );
            }
        }
        // return AliyunOSSConstants.COMMON_URL + fileUrl;
        return fileUrl;
    }

    /**
     * 删除文件
     */
    public static String delete(String fileUrl) {
        System.out.println("------OSS文件删除开始--------" + fileUrl);

        String endPoint = AliyunOSSConstants.END_POINT;
        String accessKeyId = AliyunOSSConstants.ASSCESS_KEY_ID;
        String accessKeySecret = AliyunOSSConstants.ASSCESS_KEY_SECRET;
        String bucketName = AliyunOSSConstants.BUCKET_NAME;
        OSS client = new OSSClientBuilder( ).build(endPoint, accessKeyId, accessKeySecret);
        try {
            client.deleteObject(bucketName, fileUrl);
            System.out.println("------OSS文件删除成功------" + AliyunOSSConstants.COMMON_URL + fileUrl);
        } catch (OSSException oe) {
            oe.printStackTrace();
        } catch (ClientException ce) {
            ce.printStackTrace();
        } finally {
            if (client != null) {
                client.shutdown( );
            }
        }
        return AliyunOSSConstants.COMMON_URL + fileUrl;
    }
}
